package com.konsung.ksdc.manager.appmain;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;

import com.githang.statusbar.StatusBarCompat;
import com.konsung.ksdc.R;
import com.konsung.ksdc.common.utils.Constants;
import com.konsung.ksdc.common.utils.UpgradeUtils;
import com.konsung.ksdc.config.MyApplication;
import com.konsung.ksdc.service.MqttService;

/**
 * Created by Administrator on 2016/3/7 0007.
 */
public class LauncherActivity extends MainActivity {
    private AlertDialog.Builder exitDialog;
    private UpgradeUtils upgradeUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //创建更新对象
        upgradeUtils = new UpgradeUtils(this);
        initView();
        getScreenSize();
        //检查下载更新
        upgradeUtils.findUpdateMessage();
        StatusBarCompat.setStatusBarColor(this, Color.parseColor("#45C01A"),
                true);
    }

    /**
     * 获取屏幕宽高
     */
    private void getScreenSize() {
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        Constants.screenWidth = dm.widthPixels;
        Constants.screenHeight = dm.heightPixels;
    }

    /**
     * 初始化view
     */
    private void initView() {
        exitDialog = new AlertDialog.Builder(LauncherActivity.this);
        exitDialog.setTitle(getString(R.string.btn_exit));
        exitDialog.setMessage(getString(R.string.dialog_app_exit_message));
        exitDialog.setPositiveButton(getString(R.string.btn_yes), new
                DialogInterface.OnClickListener() { //退出按钮
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        Intent intent = new Intent(LauncherActivity.this, MqttService.class);
                        stopService(intent);
                        LauncherActivity.this.finish(); //关闭activity

                    }
                });
        exitDialog.setNegativeButton(getString(R.string.btn_no), new
                DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        //不退出不用执行任何操作
                    }
                });
    }

    @Override
    protected void onResume() {
        super.onResume();
        upgradeUtils.isInApp = true; //在app界面
        if (upgradeUtils.isDowned) {
            //如果已经下载，检查安装
            upgradeUtils.checkDownloadCache();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁的时候停止下载
        upgradeUtils.downApkTask.stop();
        Constants.isupdating = false;
        if (upgradeUtils.progressDialog != null) {
            upgradeUtils.progressDialog.dismiss();
        }
        if (upgradeUtils.wifiReceiver != null) {
            unregisterReceiver(upgradeUtils.wifiReceiver);
            upgradeUtils.wifiReceiver = null;
        }
    }

    @Override
    public void onBackPressed() {
        MyApplication myApplication = (MyApplication) getApplication();
        if (myApplication.getReloginPopupWindow().isShowing()) {
            return;
        }

        if (exitDialog != null) {
            exitDialog.show();
        }
    }
}
